import {expect} from 'chai';
import * as sinon from 'sinon';
import * as step from '../../src';
import proxyquire from 'proxyquire'


let remoteStorage: RemoteStorage;
let statStub: sinon.SinonStub;
let putStub: sinon.SinonStub;
let getStub: sinon.SinonStub;
let headStub: sinon.SinonStub;

const sandbox = sinon.createSandbox();

import {skipDownload, skipUpload} from '../../src/oss/skip'
import {RemoteStorage} from '../../src/oss'
import {describe} from 'mocha'

describe('skipUpload', () => {
    beforeEach(() => {
        statStub = sandbox.stub();
        putStub = sandbox.stub();
        getStub = sandbox.stub();
        headStub = sandbox.stub();
        remoteStorage = {
            put: putStub,
            get: getStub,
            head: headStub,
            stat: statStub
        }
    });

    afterEach(() => {
        sandbox.restore();
    });

    it('should not skip upload when local is newer', async () => {
        const result = {
            res:{
                status: 200,
                headers:
                {
                    'last-modified': 'Fri, 09 Aug 2024 03:55:08 GMT'
                }
            }
        }
        headStub.resolves(result);
        const time = new Date()
        const skip = await skipUpload(remoteStorage,  "test.file", time)
        expect(skip).to.be.false;
    })


    it('should skip upload if remote is newer', async () => {
        const result = {
            status: 200,
            res:{
                headers:
                    {
                        'last-modified': 'Fri, 09 Aug 2024 03:55:08 GMT'
                    }
            }
        }
        headStub.resolves(result);
        const time = new Date('Fri, 09 Aug 2024 03:55:08 GMT')
        const skip = await skipUpload(remoteStorage,  "test.file", time)
        expect(skip).to.be.true;
    })
})


describe('skipDownload', () => {
    beforeEach(() => {
        statStub = sandbox.stub();
        putStub = sandbox.stub();
        getStub = sandbox.stub();
        headStub = sandbox.stub();
        remoteStorage = {
            put: putStub,
            get: getStub,
            head: headStub,
            stat: statStub
        }
    });

    afterEach(() => {
        sandbox.restore();
    });

    it('should skip download when local is newer', async () => {
        const result = {
            res:{
                status: 200,
                headers:
                    {
                        'last-modified': 'Fri, 09 Aug 2024 03:55:08 GMT'
                    }
            }
        }
        headStub.resolves(result);
        const time = new Date()
        const skip = await skipDownload(remoteStorage,  "test.file", time)
        expect(skip).to.be.true;
    })


    it('should not skip upload if remote is newer', async () => {
        const result = {
            status: 200,
            res:{
                headers:
                    {
                        'last-modified': 'Fri, 09 Aug 2024 04:55:08 GMT'
                    }
            }
        }
        headStub.resolves(result);
        const time = new Date('Fri, 09 Aug 2024 03:55:08 GMT')
        const skip = await skipDownload(remoteStorage,  "test.file", time)
        expect(skip).to.be.false;
    })
})




